package com.qf.shiro2202day02.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.shiro2202day02.user.dao.*;
import com.qf.shiro2202day02.user.entity.*;
import com.qf.shiro2202day02.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.shiro2202day02.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dong_
 * @since 2022-10-25
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    TUserRoleMapper userRoleMapper;

    @Autowired
    TRoleMapper tRoleMapper;

    @Autowired
    TRolePermsMapper tRolePermsMapper;

    @Autowired
    TPersMapper tPersMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void regist(User user) {

        //1. 密码生成

        final String salt = SaltUtils.getSalt(8);

        final Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);

        final String shiroPassword = md5Hash.toHex();

        user.setSalt(salt);

        user.setPassword(shiroPassword);

        this.save(user);


    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getUserRole(User user) {

        final QueryWrapper<TUserRole> wrapper = new QueryWrapper<>();

        wrapper.eq("userid",user.getId());

        final List<TUserRole> tUserRoles = userRoleMapper.selectList(wrapper);

        // 查询 角色 id 和 角色名字的对应关系
        final List<TRole> roles = tRoleMapper.selectList(null);

        // List<TRole> ---->  Map<Integer,String>
        final HashMap<Integer, String> roleMap = new HashMap<>();

        roles.forEach(tRole -> {
            roleMap.put(tRole.getId(),tRole.getName());
        });

        // List<TUserRole> ----->  List<String>  String是角色的名字
        final List<String> list = tUserRoles.stream()
                .map(tUserRole -> roleMap.get(tUserRole.getId()))
                .collect(Collectors.toList());

        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getPermission(User user) {

        //1. 根据用户id 查询 t_user_role ，得到 List<角色ID>
        final QueryWrapper<TUserRole> wrapper = new QueryWrapper<>();

        wrapper.select("roleid");
        wrapper.eq("userid",user.getId());

        final List<Object> roleIdList = userRoleMapper.selectObjs(wrapper);

        //2. 根据 List<角色ID> 查 t_role_pers ,得到 List<权限ID>

        final QueryWrapper<TRolePerms> wrapper1 = new QueryWrapper<>();

        wrapper1.select("permsid");
        wrapper1.in("roleid",roleIdList);

        final List<Object> permsIdList = tRolePermsMapper.selectObjs(wrapper1);

        // 把persmIdList进行去重
        final HashSet<Object> set = new HashSet<>();
        set.addAll(permsIdList);


        //3. List<权限ID>  ---->   List<权限字符串>
        final List<TPers> tPersList = tPersMapper.selectList(null);

        // List<TPers> ---->  Map<权限ID，权限url>
        final HashMap<Integer, String> hashMap = new HashMap<>();

        for (TPers pers : tPersList) {
            hashMap.put(pers.getId(),pers.getUrl());
        }

        final ArrayList<String> permsUrlList = new ArrayList<>();
        for (Object o : set) {
            final Integer persId = (Integer) o;
            permsUrlList.add(hashMap.get(persId));
        }

        return permsUrlList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getUserRoleBySql(User user) {

        List<String> list = userDao.getUserRoleBySql(user.getId());

        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getPermissionBysql(User user) {

        List<String> list = userDao.getPermissionBysql(user.getId());

        return list;
    }
}
